﻿using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.Helper
{
    public class Helpers
    {
        public static T GetEnumeration<T>(string description) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enum!");
            }

            foreach (T value in Enum.GetValues(typeof(T)))
            {
                if ((value as Enum).GetDescription() == description)
                {
                    return value;
                }
            }
            return default(T);
        }

        public static Enum GetEnumeration(string description, Type t)
        {
            if (!t.IsEnum)
            {
                throw new ArgumentException("T must be an enum!");
            }

            foreach (var value in Enum.GetValues(t))
            {
                if ((value as Enum).GetDescription() == description)
                {
                    return (value as Enum);
                }
            }
            return null;
        }

        public static bool ParseHexadecimalValue(string value)
        {
            char[] hexa = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
                'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' };
            foreach (var c in value.ToCharArray())
            {
                if (!hexa.Contains(c))
                {
                    return false;
                }
            }
            return true;
        }
    }
}
